/*
 * Copyright (C) 2012 artit91
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package rpg.entities.factories;

import java.awt.image.BufferedImage;
import java.util.logging.Logger;
import rpg.Events;
import rpg.Location;
import rpg.Maps;
import rpg.entities.Sprite;
import rpg.entities.eventhandlers.PeacefulRandomMovement;
import rpg.entities.properties.CreatureProperty;
import rpg.gfx.gui.Bar;
import rpg.gfx.Bitmap;
import rpg.util.ImageUtil;

/**
 *
 * @author artit91
 */
public class CritterFactory extends SpriteFactory {
    /*Singleton*/
    private static CritterFactory instance = null;
    private CritterFactory() {
        super();
    }
    
    public synchronized static void newInstance(final char c, final Maps.Map map, final int loc) {
        instance = new CritterFactory();
        instance.setProp(new CreatureProperty("Critter",
                    1,
                    1,
                    1,
                    new Location(loc, map),
                    null,
                    0,
                    0,
                    1,
                    0,
                    0
                    )
        );
        instance.setMap(map);
        instance.healthBar = new Bar(32, 6, 0xff0000, 0x1, ((CreatureProperty)instance.getProp()).getMaxHealth(), ((CreatureProperty)instance.getProp()).getHealth());
        if(c == 'c') {
            instance.bitmaps = new Bitmap[12];
            BufferedImage image = ImageUtil.createImageIcon("/res/chick.png");
            for (int i = 0; i < 12; i++) {
                int col = i % 3;
		int row = i / 3;
		instance.bitmaps[i] = new Bitmap(64, 64);
		image.getSubimage(col * 64, row * 64, 64, 64).getRGB(0, 0, instance.bitmaps[i].width, instance.bitmaps[i].height, instance.bitmaps[i].pixels, 0, instance.bitmaps[i].width);
            }
        }
        else if(c == 'r') {
            instance.bitmaps = new Bitmap[12];
            BufferedImage image = ImageUtil.createImageIcon("/res/rooster.png");
            for (int i = 0; i < 12; i++) {
                int col = i % 3;
		int row = i / 3;
		instance.bitmaps[i] = new Bitmap(64, 64);
		image.getSubimage(col * 64, row * 64, 64, 64).getRGB(0, 0, instance.bitmaps[i].width, instance.bitmaps[i].height, instance.bitmaps[i].pixels, 0, instance.bitmaps[i].width);
            }
        }
        instance.setEh(new PeacefulRandomMovement(instance));
        SpriteFactory.newInstance(instance);
    }
    
    @Override
    public Bitmap getDrawGraphics() {
	return getEh().getDrawGraphics();
    }
    /**
     * 
     * @param causer The entity that caused the damage
     * @param dmg The amount of the damage
     * @return true if the entity was killed
     */
    public boolean damage(Sprite causer, int dmg) {
        if (((CreatureProperty)instance.getProp()).getHealth() - dmg <= 0) {
                ((CreatureProperty)instance.getProp()).setHealth(0);
        } else {
                ((CreatureProperty)instance.getProp()).setHealth(((CreatureProperty)instance.getProp()).getHealth() - dmg);
                healthBar.setVal(((CreatureProperty)instance.getProp()).getHealth());
        }
        //lastDamagedBy = causer;
        return ((CreatureProperty)instance.getProp()).getHealth() == 0;
    }
    private static final Logger LOG = Logger.getLogger(CritterFactory.class.getName());
}
